/**
 * 总排行榜
 */
class rankingSub extends eui.Component{
    private title:eui.Image;
    private return:eui.Button;
    private btn:eui.Button;
    private index:eui.Label;
    private rankScroll:eui.Scroller;
    private rankList:eui.List;

    private _arrayData:eui.ArrayCollection;
    private _index:number = 0;

    private _rankData:Array<any> = [];
    private _version:number = 0;
    private _allversion:number = 0;
    private _friendversion:number = 0;
    private _friend:boolean = false;
    public constructor(){
        super();
       // this.cacheAsBitmap =true;
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "rankingSkin"; 
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.inIt();
    }

    private inIt(){
        this.return.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.dispatchEventWith("return");
        },this);
        this.btn.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(this._friend){
                //跳转总榜页面
                this.title.source = "bg_icon_rank_png";
                this.btn.label = "好友排行榜";
                this._version = this._allversion;
            }else{
                //跳转朋友排行榜页面
                this.title.source = "bg_icon_rank_friend_png";
                this.btn.label = "总榜";        
                this._version = this._allversion;       
            }
            this._friend = !this._friend;
            this.getVersion();
        },this);

        Dispatcher.ins.addEventListener("rankVersion",this.resetVersion,this);
        Dispatcher.ins.addEventListener("rankList",this.arrayData,this);
        Dispatcher.ins.addEventListener("deleteFriend",this.getVersion,this);
        Dispatcher.ins.addEventListener("addFriendList",this.getVersion,this);

        this._arrayData =  new eui.ArrayCollection(this._rankData);
        this.rankList.dataProvider = this._arrayData;
        this.rankList.itemRenderer = rankIR;
        // this.rankList.addEventListener(eui.ItemTapEvent.ITEM_TAP,(evt:egret.Event)=>{
        //     var dataList:eui.List = evt.target;
        //     var item = dataList.selectedItem;
        //     let userId = item.UserId;
        //     if(userId !== common.userInfo.userId){
        //         Dispatcher.ins.dispatchEvent(new GameEvent("openOtherHome",{"isFriend":0,"userId":userId}));
        //     }else{
        //         this.dispatchEventWith("return");
        //     }
        // },this);
        this.rankList.addEventListener("openHome",(evt:GameEvent)=>{
            var dataList:eui.List = evt.target;
            var item = dataList.selectedItem;
            let userId = item.UserId;
            if(userId !== common.userInfo.userId){
                Dispatcher.ins.dispatchEvent(new GameEvent("openOtherHome",{"isFriend":0,"userId":userId}));
            }else{
                this.dispatchEventWith("return");
            }
        },this);
        this.getVersion();
    }
    public getVersion(){
        let dataPost:any = {};
        if(this._friend){
            dataPost.type = 1;
        }else{
            dataPost.type = 0;
        }
        socket.request("rankVersion",dataPost);//获取排行榜数据
    }
    public resetVersion(evt:GameEvent){
        let obj = evt.data;
        if(this._friend){
            this._friendversion = obj.version;
        }else{
            this._allversion = obj.version;
        }
        this._version = obj.version;
        this.getList();
    }
    public getList(){
        let dataPost:any = {"version":this._version};
        if(this._friend){
            dataPost.type = 1;
        }else{
            dataPost.type = 0;
        }
        socket.request("rankList",dataPost);
    }
    public arrayData(evt:GameEvent){
        let obj = evt.data;
        let index = obj.myIndex;
        if(index == 0){
            this.index.text = "暂未上榜";
        }else{
            this.index.text = index + "";
        }
        this._rankData = obj.list;
        this._arrayData =  new eui.ArrayCollection(this._rankData);
        this.rankList.dataProvider = this._arrayData;
        this._arrayData.refresh();
    }
}

class rankIR extends eui.ItemRenderer{
    private nickName:eui.Label;
    private trophy:eui.Image;//前三名显示
    private rankGroup:eui.Group;//排名
    private bgMask:eui.Image;
    private head:eui.Image;
    private source:Array<any> = ["icon_rank_gold_png","icon_rank_silver_png","icon_rank_bronze_png"];
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "rankIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.head.mask = this.bgMask;
        let status = "";
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
            status = "begin";
        },this);
        this.addEventListener(egret.TouchEvent.TOUCH_MOVE,()=>{
            status = "move";
        },this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(status !== "move"){
                this.parent.dispatchEventWith("openHome",true,this.data);
            }
        },this);
    }
    protected dataChanged():void{
       if(this.data.Index <= 3){
           this.rankGroup.visible = false;
           this.trophy.visible = true;
           this.trophy.source = this.source[this.data.Index - 1];
       }else{
           this.rankGroup.visible = true;
           this.trophy.visible = false;
       }
       if(this.nickName.text.length > 14){
           this.nickName.text = this.nickName.text.substr(0,13) + "...";
       }
    }
}